import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;

public class Main {

	public static void main(String[] args) {
		if (args.length == 2 || args.length == 1) {
			ObjParser pars = new ObjParser(args[0]);
			Mesh m = null;
			try {
				m = pars.loadMesh();
			} catch (ParseException e) {
				System.err.println("Error parsing file:");
				e.printStackTrace();
				System.exit(1);
			}

			FileWriter fw = null;
			try {
				if (args.length == 2)
					fw = new FileWriter(args[1]);
				else
					fw = new FileWriter(args[0] + ".moddl");
				fw.write("name " + m.name + "\n");
				fw.write("matlib " + m.matLib + "\n");
				fw.write("matname " + m.matName);
				for (int i = 0; i < m.vertexList.length; i++) {
					fw.write("\nv");
					for (int y = 0; y < m.vertexList[i].length; y++) {
						fw.write(" " + m.vertexList[i][y]);
					}
				}
				for (int i = 0; i < m.indexList.length; i++) {
					fw.write("\ni");
					for (int y = 0; y < m.indexList[i].length; y++) {
						fw.write(" " + m.indexList[i][y]);
					}
				}
			} catch (IOException ioexc) {
				ioexc.printStackTrace();

			} finally {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			System.err.println("Wrong Argument Count: "+args.length);
		}
	}

}
